#include <CoOS.h>			                 /*!< CoOS header file            */
#include <stdio.h>
#include "NUC123.h"
#include "slc.h"
#include "shell.h"

U8 u8SLCMode;
OS_TCID tid;

static slcfun(void)
{
	
	tid = CFG_MAX_TMR;
	if(u8SLCMode == SLC_OFF)u8SLCMode = SLC_ON;
	if(u8SLCMode == SLC_ON)u8SLCMode = SLC_OFF;
	switch(u8SLCMode)
	{
		case SLC_OFF:
			GPIO_SetMode(PC, BIT5, GPIO_PMD_OPEN_DRAIN);
			PC5 = 1;
			break;
		case SLC_ON:
			GPIO_SetMode(PC, BIT5, GPIO_PMD_OUTPUT);
			PC5 = 0;
			break;
		default:
			break;
	}
}

void Slc_Init(void)
{
	GPIO_SetMode(PC, BIT5, GPIO_PMD_OPEN_DRAIN);
	u8SLCMode = SLC_OFF;
	PC5 = 1;
	tid = CFG_MAX_TMR;
}

void Slc_Set(U8 u8Mode, U32 u32Time)
{
	
	u8SLCMode = u8Mode;
	switch(u8SLCMode)
	{
		case SLC_OFF:
			GPIO_SetMode(PC, BIT5, GPIO_PMD_OPEN_DRAIN);
			PC5 = 1;
			break;
		case SLC_ON:
			GPIO_SetMode(PC, BIT5, GPIO_PMD_OUTPUT);
			PC5 = 0;
			break;
		default:
			break;
	}

	if(u32Time != 0)
	{
		u8SLCMode = u8Mode;
		tid = CoCreateTmr(TMR_TYPE_ONE_SHOT, u32Time*1000, 0, slcfun);
		CoStartTmr(tid);
	}
	
}

U8 Slc_GetOnoff(void)
{
	return u8SLCMode;
}

U32 Slc_GetTime(void)
{
	if(tid<CFG_MAX_TMR)
	{
		StatusType err;
		return CoGetCurTmrCnt(tid, &err);
	}

	return 0;
}

void slc_test(char *p0, char *p1, char *p2)
{
	U8 u8cmd = atoi(p0);
	U32 u32Time = atoi(p1);
	switch(u8cmd)
	{
		case 0:
			printf("scl on\n");
			Slc_Set(SLC_ON, 0);
			break;
		case 1:
			printf("scl off\n");
			Slc_Set(SLC_OFF, 0);
			break;
		default:
			break;
	}
}

SHELL_CMD(slc, slc_test);
